#include "get_arg.h"
#include <iostream>
#include <simple_suffix_array.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <portable_timer.h>
#include <get_filename.h>
#include <print_error.h>
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
#include <cstdlib>


using namespace std;
using namespace bcpp_matrix;
using boost::lexical_cast;
using std::ostringstream;
using std::istringstream;
using std::ofstream;
using std::ifstream;


string nuc = "ACGT";
unsigned LENGTH = 100;
string TUPLE = "CACAAGAACACCAA";
unsigned TUPLE_LEN = 4;
unsigned BEGIN = 2;
unsigned END = BEGIN + TUPLE_LEN + 2;
unsigned SEARCH_BUF_END_OFFSET = 2;
int main (int argc, char *argv[])
{
	t_program_args args;
	try
	{
		srand(1234);
		if (!process_command_line_options(argc, argv, args))
			return 1;
		string str;
		str.reserve(LENGTH);
		if (!TUPLE_LEN)
			TUPLE_LEN = TUPLE.length();
		//for (unsigned i = 0; i < LENGTH; ++i)
		//	str += nuc[rand() % 3];
		str = "ACAAGATGGCCAAGTTTG";
		//     012345678901234567
		std::cerr << str << "\n";
		
		std::cerr << "Initialized string\n";
		t_simple_suffix_array suffix_array (str, TUPLE_LEN);
		std::cerr << "Created suffix array\n";

		for (unsigned i = 0; i < str.length() +1; ++i)
		{
			unsigned res = suffix_array.find_first_of(TUPLE, 
													   i, str.length() - SEARCH_BUF_END_OFFSET, 
													   BEGIN, END);
			if (res != std::string::npos)
			{
				std::cerr
				<< res
				<< "\tstarting from " << i <<"=\n" << str.substr(res,  END - BEGIN)
				<< "\n" << TUPLE.substr(BEGIN, END - BEGIN) << "\n";
				for (unsigned i = 0; i < (END - BEGIN); ++i)
					std::cerr << (str[res + i] == TUPLE[i + BEGIN] ? ' ' : '^');
				std::cerr << "\n";
				//for (unsigned i = 0; i < (END - BEGIN); ++i)
				//	std::cerr << (i % 10);
				//std::cerr << "\n";
			}

			res = suffix_array.find_last_of(TUPLE, 
													   i, str.length() - SEARCH_BUF_END_OFFSET, 
													   BEGIN, END);
			if (res != std::string::npos)
			{
				std::cerr
				<< res
				<< "\trev starting from " << i <<"=\n" << str.substr(res,  END - BEGIN)
				<< "\n" << TUPLE.substr(BEGIN, END - BEGIN) << "\n";
				for (unsigned i = 0; i < (END - BEGIN); ++i)
					std::cerr << (str[res + i] == TUPLE[i + BEGIN] ? ' ' : '^');
				std::cerr << "\n";
				//for (unsigned i = 0; i < (END - BEGIN); ++i)
				//	std::cerr << (i % 10);
				//std::cerr << "\n";
			}
			//else
			//	std::cerr
			//	<< "NONE\tstarting from " << i << "\n";
		}

		for (unsigned i = 0; i < str.length(); ++i)
		{
			if (str.length() - i  == 4)
				cerr << "\n";
			unsigned res = suffix_array.find_first_of(TUPLE, 
													   0, str.length() - i, 
													   BEGIN, END);
			if (res != std::string::npos)
			{
				std::cerr
				<< res
				<< "\tfor 0 " << "->" << str.length() - i <<"=\n" << str.substr(res,  END - BEGIN)
				<< "\n" << TUPLE.substr(BEGIN, END - BEGIN) << "\n";
				for (unsigned i = 0; i < (END - BEGIN); ++i)
					std::cerr << (str[res + i] == TUPLE[i + BEGIN] ? ' ' : '^');
				std::cerr << "\n";
				//for (unsigned i = 0; i < (END - BEGIN); ++i)
				//	std::cerr << (i % 10);
				//std::cerr << "\n";
			}

			res = suffix_array.find_last_of(TUPLE, 
											   0, str.length() - i, 
											   BEGIN, END);
			if (res != std::string::npos)
			{
				std::cerr
				<< res
				<< "\trev 0 " << "->" << str.length() - i <<"=\n" << str.substr(res,  END - BEGIN)
				<< "\n" << TUPLE.substr(BEGIN, END - BEGIN) << "\n";
				for (unsigned i = 0; i < (END - BEGIN); ++i)
					std::cerr << (str[res + i] == TUPLE[i + BEGIN] ? ' ' : '^');
				std::cerr << "\n";
				//for (unsigned i = 0; i < (END - BEGIN); ++i)
				//	std::cerr << (i % 10);
				//std::cerr << "\n";
			}
			//else
			//	std::cerr
			//	<< "NONE\tstarting from " << i << "\n";
		}
		

	}
	catch (std::exception& e)
	{
		std_print_error(args.CERR(),  e.what());
		return 1;

	}
	return 0;






	cerr << "\n";
	cerr << string(10, '!') << "\nEverything worked!!\n";

	return 0;
}


